package com.css.zfzx.sjcj.modules.pchjdzb.service.impl;

import com.css.bpm.platform.utils.PlatformDateUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.sjcj.modules.pchjdzb.repository.PcHjDzbRepository;
import com.css.zfzx.sjcj.modules.pchjdzb.repository.entity.PcHjDzbEntity;
import com.css.zfzx.sjcj.modules.pchjdzb.service.PcHjDzbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;

/**
 * @Description: zfzx-wxqh
 * @Author: lhl
 * @CreateDate: 2022/4/24 17:22
 */
@Service
public class PcHjDzbServiceImpl implements PcHjDzbService {
    @Autowired
    PcHjDzbRepository repository;

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 数据源
     * 10.19.41.111
     */
    @Value("${spring.datasource.url}")
    private String url = "";
    @Value("${spring.datasource.username}")
    private String name = "";
    @Value("${spring.datasource.password}")
    private String pwd = "";

    /**
     * 获取数据库连接
     * SQL语句
     */
    private Connection getConnection() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, name, pwd);
        return conn;
    }

    @Override
    @Transactional
    public void save(String province, String city, String area, String dataType) {
        PcHjDzbEntity entity = new PcHjDzbEntity();
        String uuid = UUIDGenerator.getUUID();
        entity.setUuid(uuid);
        entity.setProvince(province);
        entity.setCity(city);
        entity.setArea(area);
        entity.setDataType(dataType);
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.repository.save1(entity);
    }

    public void jdbcSave(String province, String city, String area, String dataType) {
        /*String sql = "insert into pc_hj_dzb" +
                " values (?,?,?,?,?,?,?,?)";

        jdbcTemplate.update(sql, UUIDGenerator.getUUID(),
                province,
                city,
                area,
                PlatformDateUtils.getCurrentTimestamp(),
                dataType,
                "",
                "");*/

        String id = UUIDGenerator.getUUID();
        Timestamp createTime = PlatformDateUtils.getCurrentTimestamp();
        String extends1 = "";
        String extends2 = "";
        String sql_str = "insert into pc_hj_dzb values ('" + id + "','" + province + "','" + city + "','" + area + "','" +
                createTime + "','" + dataType + "','" + extends1 + "','" + extends2 + "')";
        try{
            Connection conn = getConnection();
            PreparedStatement ps = conn.prepareStatement(sql_str);
            ps.executeUpdate(sql_str);
            if (ps != null) ps.close();
            if (conn != null) conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
